package _15_generics._15_4.exercise;

import javax.swing.*;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;

/**
 * Study the JDK documentation for EnumSet. You'll
 * see there's a clone() method.
 * However,you cannot clone() from the reference
 * to the set interface passed in Sets.java. Can you
 * modify Sets.java to handle both the general case
 * of a Set interface as shown, and the special case
 * of an EnumSet, using clone() instead of creating
 * a new HashSet.
 */
public class E17_Sets2 {
}

class Sets2{
    protected static <T> Set<T> copy(Set<T> s){
        if (s instanceof EnumSet){
            return ((EnumSet) s).clone();
        }
        return new HashSet<>(s);
    }

    public static <T> Set<T> union(Set<T> a,Set<T> b){
        Set<T> result = copy(a);
        result.addAll(b);
        return result;
    }
}
